#include "slSphere.hh"

slSphere::slSphere(int radius)
{
	_radius = radius;
	space = 1;
	VertexCount = (90 / space+1) * (360 / space+1) * 4;
	vertices = new Vertices[VertexCount];

	btVector3 	localInertia(0, 0, 0);
	shape = new btSphereShape(1);
	myTransform.setIdentity();
	myTransform.setOrigin( btVector3(0,5,0) );
	mass = 0.5f;
	if (mass)
		shape->calculateLocalInertia(mass, localInertia);
	myMotionState = new btDefaultMotionState(myTransform);
	btRigidBody::btRigidBodyConstructionInfo myBoxRigidBodyConstructionInfo( mass, myMotionState, shape, localInertia );
	body = new btRigidBody(myBoxRigidBodyConstructionInfo);
	createSphere(100, 0, 0, 0);
}

slSphere::slSphere(int x, int y, int z, int radius)
{
	_radius = radius;
	space = 1;
	VertexCount = (90 / space+1) * (360 / space+1) * 4;
	vertices = new Vertices[VertexCount];

	btVector3 	localInertia(0, 0, 0);
	shape = new btSphereShape(1);
	myTransform.setIdentity();
	myTransform.setOrigin(btVector3(x,y,z) );
	mass = 0.5f;
	if (mass)
		shape->calculateLocalInertia(mass, localInertia);
	myMotionState = new btDefaultMotionState(myTransform);
	btRigidBody::btRigidBodyConstructionInfo myBoxRigidBodyConstructionInfo( mass, myMotionState, shape, localInertia );
	body = new btRigidBody(myBoxRigidBodyConstructionInfo);
	createSphere(50, 0, 0, 0);
}

void slSphere::createSphere(double r, double h, double k, double z)
{
	int n = 0;


	for (double b = 0; b <= 90 - space; b += space)
	{
		for (double a = 0; a <= 360 - space; a += space)
		{
			vertices[n].x = r * sin((a) / 180 * DOUBLE_PI) * sin((b) / 180 * DOUBLE_PI) - h;
			vertices[n].y = r * cos((a) / 180 * DOUBLE_PI) * sin((b) / 180 * DOUBLE_PI) + k;
			vertices[n].z = r * cos((b) / 180 * DOUBLE_PI) - z;
			vertices[n].v = (2 * b) / 360;
			vertices[n].u = a / 360;
			n++;

			vertices[n].x = r * sin((a) / 180 * DOUBLE_PI) * sin((b + space) / 180 * DOUBLE_PI) - h;
			vertices[n].y = r * cos((a) / 180 * DOUBLE_PI) * sin((b + space) / 180 * DOUBLE_PI) + k;
			vertices[n].z = r * cos((b + space) / 180 * DOUBLE_PI) - z;
			vertices[n].v = (2 * (b + space)) / 360;
			vertices[n].u = (a) / 360;
			n++;

			vertices[n].x = r * sin((a + space) / 180 * DOUBLE_PI) * sin((b) / 180 * DOUBLE_PI) - h;
			vertices[n].y = r * cos((a + space) / 180 * DOUBLE_PI) * sin((b) / 180 * DOUBLE_PI) + k;
			vertices[n].z = r * cos((b) / 180 * DOUBLE_PI) - z;
			vertices[n].v = (2 * b) / 360;
			vertices[n].u = (a + space) / 360;
			n++;

			vertices[n].x = r * sin((a + space) / 180 * DOUBLE_PI) * sin((b + space) / 180 * DOUBLE_PI) - h;
			vertices[n].y = r * cos((a + space) / 180 * DOUBLE_PI) * sin((b + space) / 180 * DOUBLE_PI) + k;
			vertices[n].z = r * cos((b + space) / 180 * DOUBLE_PI) - z;
			vertices[n].v = (2 * (b + space)) / 360;
			vertices[n].u = (a + space) / 360;
			n++;
		}
	}
}

void slSphere::draw()
{
	myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(matrix);
	glPushMatrix();
	glMultMatrixf(matrix);

	glEnable(GL_COLOR_MATERIAL);

	glScalef (0.02 * _radius, 0.02 * _radius, 0.02 * _radius);
	glRotatef (90, 1, 0, 0);


	glBegin(GL_TRIANGLE_STRIP);
	for (int b = 0; b < VertexCount; b++)
	{
		glTexCoord2f(vertices[b].u, vertices[b].v);
		glVertex3f(vertices[b].x, vertices[b].y, -vertices[b].z);
	}
	glEnd();

	glFrontFace(GL_CW);
	glBegin (GL_TRIANGLE_STRIP);
	for (int b = 0; b < VertexCount; b++)
	{
		glTexCoord2f (vertices[b].u, -vertices[b].v);
		glVertex3f (vertices[b].x, vertices[b].y,
					vertices[b].z - 2 * vertices[0].z - 2 * (vertices[(int)VertexCount-1].z - vertices[0].z));
	}
	glEnd();
	glPopMatrix();
}

slSphere::~slSphere()
{
	delete[] vertices;
}